Skip to content

Skip remote content upload when the remote cache is read-only - #178

Open
Pavlo Tubolets (tubolets) wants to merge 3 commits into
microsoft:mainfrom
tubolets:fix/skip-remote-content-upload-when-readonly
Open

Skip remote content upload when the remote cache is read-only#178
Pavlo Tubolets (tubolets) wants to merge 3 commits into
microsoft:mainfrom
tubolets:fix/skip-remote-content-upload-when-readonly

Conversation

@tubolets

Copy link
Copy Markdown

RemoteCacheIsReadOnly suppresses content hash list publication but not content upload, so a read-only client uploads content that no later build can ever retrieve — nothing references it. It costs egress, storage and build time for no benefit.

CasCacheClient holds both the raw _remoteCacheSession and the _twoLevelCacheSession built from TwoLevelCacheConfiguration. AddNodeAsync uploads content through the raw session without consulting the flag, while the content hash list goes through the two-level session, which honours it.

Changes

  • Capture RemoteCacheIsReadOnly from the passed TwoLevelCacheConfiguration and skip the remote upload block when it is set. This also elides the remote PinBulkAsync calls, which were made only to decide what to upload.
  • Read path is unchanged.

Validation

  • dotnet build MSBuildCache.sln -c Release: succeeded, 0 warnings, 0 errors
  • dotnet test MSBuildCache.sln -c Release --no-build: 547/550 passed. The 3 net472 failures are a pre-existing The tools version "Current" is unrecognized environment issue in MSBuildCachePluginBaseTests; an unmodified checkout fails the same 3.

No unit test added: there is no mock ICacheSession in Common.Tests to build an AddNodeAsync harness on, and CasCacheClientTests currently only covers the static GetAddNodeResult helper. Happy to add a mock session and a regression test if you'd like it in this PR.

Fixes: #177

CasCacheClient holds two sessions: the raw _remoteCacheSession and the
_twoLevelCacheSession built from TwoLevelCacheConfiguration. When adding a
node, content was uploaded directly through _remoteCacheSession without
consulting RemoteCacheIsReadOnly, while the content hash list that would
reference that content is published through _twoLevelCacheSession, which does
honor it.

The result is that a read-only client uploads content that can never be
retrieved: no content hash list ever points at it, so no subsequent build can
hit on it. The upload only costs egress, storage and build time, and leaves
unreferenced blobs behind for lifecycle policies to collect.

Guard the upload block on the read-only flag, which also elides the remote
pin calls that were only made to decide what to upload.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

// this content is not published either (TwoLevelCacheSession honors RemoteCacheIsReadOnly below), so any
// content uploaded here could never be retrieved by a subsequent build. Uploading it would only cost
// egress, storage and time.
if (_remoteCacheSession != null && !_remoteCacheIsReadOnly)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression test that verifies AddNodeAsync makes no PinAsync or PutStreamAsync calls on the remote session, while still writing the node to the local cache. The existing CasCacheClientTests only exercise GetAddNodeResult.

@tubolets Pavlo Tubolets (tubolets) Aug 26, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — added in 9555de7.

There were no ICacheSession/ICache test doubles in Common.Tests, so I added RecordingCacheSession and RecordingCache under Mocks/. They count the calls made against them and throw on anything the tests do not exercise. The tests drive the public CacheClient.AddNodeInternalAsync, which reaches AddNodeAsync without needing outputs on disk.

Two tests:

  • AddNodeDoesNotUploadContentWhenRemoteCacheIsReadOnly — asserts the remote session sees no PinAsync, PutStreamAsync or PutFileAsync calls, and that the node metadata and content hash list are still written to the local cache.
  • AddNodeUploadsContentWhenRemoteCacheIsWritable — asserts the remote session is still pinned and uploaded to when the remote is writable, so the guard is conditional rather than someone later making it unconditional.

The local session reports pins as succeeding (so nothing needs ingesting from disk) and the remote reports them as missing (so a writable remote would upload).

I confirmed the regression test fails with the fix reverted and the tests unchanged:

Assert.AreEqual failed. Expected:<0>. Actual:<1>. 'actual' expression: 'remoteSession.PinCallCount'.
The remote session must not be pinned when the remote cache is read-only.

while AddNodeUploadsContentWhenRemoteCacheIsWritable still passed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found a small bug, addressed in the next commit

@dfederm David Federman (dfederm) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

Just 1 comment to add a regression test, but otherwise looks great!

Adds RecordingCacheSession / RecordingCache, minimal ICacheSession and ICache
implementations that count the calls made against them, and two tests driving
CasCacheClient.AddNodeInternalAsync:

- AddNodeDoesNotUploadContentWhenRemoteCacheIsReadOnly asserts the remote
  session sees no PinAsync, PutStreamAsync or PutFileAsync calls, while the node
  metadata and content hash list are still written to the local cache.
- AddNodeUploadsContentWhenRemoteCacheIsWritable asserts the remote session is
  still pinned and uploaded to when the remote cache is writable, so the guard
  is conditional rather than unconditional.

The first test fails without the accompanying fix:
Expected:<0>. Actual:<1>. 'actual' expression: 'remoteSession.PinCallCount'.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@tubolets

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="WiseTech Global"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RemoteCacheIsReadOnly still uploads content to the remote cache

2 participants